<?php

namespace app\system\controller;

use app\common\model\AuthGroupModel;
use app\common\model\AuthRuleModel;
use think\Request;

class AuthGroupController extends BaseController
{
    private $utype;
    private $authGroupModel;
    private $authRuleCache;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->utype = $this->request->param('utype');
        if (empty($this->utype)) {
            $this->error('未指定模块参数');
        }
        $this->authGroupModel = new AuthGroupModel();
        $this->authRuleCache  = cache('auth_rule_'.$this->utype);
    }

    public function index()
    {
        $utypeList = get_dict('101');
        $groupList = $this->authGroupModel->where('utype', $this->utype)->order('group_sort asc')->select();
        $this->assign('utypeList', $utypeList);
        $this->assign('groupList', $groupList);
        return $this->fetch();
    }

    /**
     * 格式化权限值
     * @param $value
     * @return string
     */
    private function formatValue($value)
    {
        if (empty($value)) {
            return '';
        }
        if(is_array($value)){
            if (in_array('all', $value)) {
                $result = 'all';
            }else{
                $result = implode(',',$value);
            }
            return $result;
        }else{
            return $value;
        }
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'AuthGroupValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $data['rules']      = $this->formatValue($data['rules']);
            $data['cms_cids']   = $this->formatValue($data['cms_cids']);
            $data['goods_cids'] = $this->formatValue($data['goods_cids']);
            $this->authGroupModel->addData($data);
            $this->success('添加成功', url('index', ['utype' => $this->utype]));
        }
        $this->assign('cmsCategory', cache('cms_category' . $this->getSiteId()));
        $this->assign('goodsCategory', cache('goods_category' . $this->getSiteId()));
        $this->assign('authRule', $this->authRuleCache);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'AuthGroupValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $data['rules']      = $this->formatValue($data['rules']);
            $data['cms_cids']   = $this->formatValue($data['cms_cids']);
            $data['goods_cids'] = $this->formatValue($data['goods_cids']);
            if ($this->authGroupModel->editData($data)) {
                $this->success('修改成功', url('index', ['utype' => $this->utype]));
            } else {
                $this->error('修改失败');
            };
        }
        $groupId = $this->request->param('group_id/d');
        $info    = $this->authGroupModel->get($groupId);
        $this->assign('cmsCategory', cache('cms_category' . $this->getSiteId()));
        $this->assign('goodsCategory', cache('goods_category' . $this->getSiteId()));
        $this->assign('authRule', $this->authRuleCache);
        $this->assign('all', 'all');
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除规则
     */
    public function del()
    {
        $groupId = $this->request->param('group_id/d');
        if ($this->authGroupModel->delData($groupId)) {
            $this->success('删除成功', url('index', ['utype' => $this->utype]));
        } else {
            $this->error('删除失败');
        };
    }

    /**
     * 更新排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $groupId => $order) {
            $groupId    = intval($groupId);
            $order      = intval($order);
            $dataList[] = ["group_id" => $groupId, "group_sort" => $order];
        }
        $this->authGroupModel->saveAll($dataList);
        $this->success('排序成功！');
    }


}